﻿<!---<cfset local.CR = 1>
<cfset local.ProgressionID = 1>

<cfswitch expression="#local.ProgressionID#">
	<cfcase value="1">
		<cfset local.CRAdjustment = .5>
	</cfcase>
	<cfcase value="2">
		<cfset local.CRAdjustment = 1>
	</cfcase>
	<cfcase value="3">
		<cfset local.CRAdjustment = 1.5>
	</cfcase>
</cfswitch>

<cfset local.WeightedCR = local.CR * local.CRAdjustment>

<cfset local.ItemValueMax = (10 + 2 * local.WeightedCR) * rand("SHA1PRNG") / 100 * local.Value>

<cfdump var="#local.ItemValueMax#">--->

<!---<cfset local.Value = 350>
<cfset local.ItemValueEach = 8>
<cfoutput>
	#local.Value / local.ItemValueEach#
	<br/>
	#int(local.Value / local.ItemValueEach)#
	<br/>
	#ceiling(local.Value / local.ItemValueEach)#
</cfoutput>--->

<cfset this.DSN = "Pathfinder">
<cffunction name="GetDieResults" access="remote" output="false">
	<cfargument name="NumberOfDice" required="true" type="numeric">
	<cfargument name="DieType" required="true" type="numeric">
	<cfargument name="Modifier" required="false" default="0" type="numeric">
	<cfargument name="ModifierOpperand" required="false" default="+" type="string">

	<cfset local.ErrorMessage = []>
	<cfset local.IsError = false>

	<cfset local.Result = StructNew()>
	<cfset local.Result.Result = 0>

	<cfloop from="1" to="#arguments.NumberOfDice#" index="local.thisRoll">
		<cfset local.Result[local.thisRoll] = RandRange(1, arguments.DieType, "SHA1PRNG")>
		<cfset local.Result.Result += local.Result[local.thisRoll]>
	</cfloop>

	<cfset local.Result.Result = evaluate(local.Result.Result & arguments.ModifierOpperand & arguments.Modifier)>

	<cfset local.Result = local.Result.Result>

	<cfreturn local.Result>
</cffunction>

	<cffunction name="GetArt" access="remote" output="false" securejson="false" displayname="GetArt" returntype="struct" description="get gems">
		<cfargument name="AverageValue" required="true" type="numeric">

		<cfset local.Art = {}>

		<cfquery name="local.ArtClassification" datasource="#this.DSN#">
			select at.Arttypeid, at.numdice, at.dietype, at.modifier, at.modifieroperand, at.cointypeid, at.averagevalue, a.Description

			from Art a
			join Arttypelinks atl on atl.Artid = a.Artid
			join ArtTypes at on at.Arttypeid = atl.Arttypeid

			where at.averagevalue <= #Max(55, arguments.AverageValue)#
			group by a.Description, at.AverageValue
			order by at.averagevalue desc, rand()
			limit 1
		</cfquery>


		<cfset local.Art.Name = local.ArtClassification.Description>
		<cfset local.Art.Price = GetDieResults(local.ArtClassification.NumDice, local.ArtClassification.DieType, local.ArtClassification.Modifier, local.ArtClassification.ModifierOperand)>

		<cfreturn local.Art>
	</cffunction>

	<cffunction name="GetCoinSplit" access="remote" output="false" securejson="false" displayname="GetCoinSplit" returntype="struct" description="get gems">
		<cfargument name="Value" required="true" type="numeric">
		<cfargument name="CR" required="false" type="numeric" default="1">

		<cfset local.Coins = {
			Platinum = 0,
			Gold = 0,
			Silver = 0,
			Copper = 0
		}>
		<cfset local.Value = arguments.Value>

		<!--- copper split --->
		<cfif local.Value gt 0>
			<cfset local.PercentCopper = rand() * (-2 * arguments.CR + 43) / 100>
			<cfset local.Coins.Copper = Int(local.Value * ( local.PercentCopper) * 100)>
			<cfset local.Value -= local.Coins.Copper / 100>
		</cfif>

		<!--- silver split --->
		<cfif local.Value gt 0>
			<cfset local.PercentSilver = rand() * (-1.6 * arguments.CR + 41.5) / 100>
			<cfset local.Coins.Silver = Int(local.Value * ( local.PercentSilver) * 10)>
			<cfset local.Value -= local.Coins.Silver / 10>
		</cfif>

		<!--- Gold split --->
		<cfif local.Value gt 0>
			<cfset local.PercentGold = rand() * (1.6 * arguments.CR + 8) / 100>
			<cfset local.Coins.Gold = Int(local.Value * local.PercentGold)>
			<cfset local.Value -= local.Coins.Gold>
		</cfif>

		<cfif local.Value gt 0>
			<cfset local.Coins.Platinum = Int(local.Value / 10)>
			<cfset local.Value -= local.Coins.Platinum * 10>
		</cfif>

		<cfset local.Coins2 = {
			Platinum = 0,
			Gold = 0,
			Silver = 0,
			Copper = 0
		}>
		<cfset local.Value2 = arguments.Value>

		<cfif local.Value2 gt 0>
			<cfset local.PercentPlatinum2 = rand() * (2 * arguments.CR + 1) / 100>
			<cfset local.Coins2.Platinum = Int(local.Value2 * local.PercentPlatinum2 / 10)>
			<cfset local.Value2 -= local.Coins2.Platinum * 10>
		</cfif>

		<!--- Gold split --->
		<cfif local.Value2 gt 0>
			<cfset local.PercentGold2 = rand() * (1.6 * arguments.CR + 8) / 100>
			<cfset local.Coins2.Gold = Int(local.Value2 * local.PercentGold2)>
			<cfset local.Value2 -= local.Coins2.Gold>
		</cfif>

		<!--- silver split --->
		<cfif local.Value2 gt 0>
			<cfset local.PercentSilver2 = rand() * (-1.6 * arguments.CR + 41.5) / 100>
			<cfset local.Coins2.Silver = Int(local.Value2 * ( local.PercentSilver2) * 10)>
			<cfset local.Value2 -= local.Coins2.Silver / 10>
		</cfif>

		<!--- copper split --->
		<cfif local.Value2 gt 0>
			<cfset local.Coins2.Copper = Int(local.Value2 * 100)>
			<cfset local.Value2 -= local.Coins2.Copper / 100>
		</cfif>

		<cfreturn local>
	</cffunction>

	<cffunction name="GetCoinsGemsArt" access="remote" output="false" displayname="GetCoinsGemsArt" returntype="any" description="get coins, gems and jewels, and art">
		<cfargument name="CR" required="true" type="numeric">
		<cfargument name="ProgressionID" required="true" type="numeric">
		<cfargument name="Value" required="true" type="numeric">

		<cfset local.Treasure = {Gems = {},
								Art = {},
								Coins = {}
								}>

		<cfswitch expression="arguments.ProgressionID">
			<cfcase value="1">
				<cfset local.CRAdjustment = .5>
			</cfcase>
			<cfcase value="3">
				<cfset local.CRAdjustment = 1.5>
			</cfcase>
			<cfdefaultcase>
				<cfset local.CRAdjustment = 1>
			</cfdefaultcase>
		</cfswitch>

		<cfset local.WeightedCR = arguments.CR * local.CRAdjustment>
		<cfset local.GemItemValueMax = (10 + 2 * local.WeightedCR) * rand("SHA1PRNG") / 100 * arguments.Value>
		<cfset local.Treasure.Gems = GetGems(GemItemValueMax)>

		<cfset local.ArtItemValueMax = (10 + 1.5 * arguments.CR) * rand("SHA1PRNG") / 100 * arguments.Value>
		<cfset local.Treasure.Art = GetArt(ArtItemValueMax)>

		<!--- todo: procedure for determining gems/art/coins
				3) randomly distribute remaining value over cointTypes, weighted towards larger coin types for higher CR and smaller coin types for lower CR
		--->

		<cfreturn local.Treasure>
	</cffunction>

	<cffunction name="GetGems" access="remote" output="false" securejson="false" displayname="GetGems" returntype="struct" description="get gems">
		<cfargument name="AverageValue" required="true" type="numeric">

		<cfset local.Gems = {}>

		<cfquery name="local.GemClassification" datasource="#this.DSN#">
			select gt.gemtypeid, gt.numdice, gt.dietype, gt.modifier, gt.modifieroperand, gt.cointypeid, gt.averagevalue, g.name

			from gemtypes gt
			join gemtypelinks gtl on gtl.gemtypeid = gt.gemtypeid
			join gems g on g.gemid = gtl.gemid

			where gt.averagevalue <= #Max(10, arguments.AverageValue)#
			order by rand()
			limit 1
		</cfquery>

		<cfset local.Gems.Name = local.GemClassification.Name>
		<cfset local.Gems.Price = GetDieResults(local.GemClassification.NumDice, local.GemClassification.DieType, local.GemClassification.Modifier, local.GemClassification.ModifierOperand)>
		<cfset local.Gems.Quantity = Max(1, IIf(GetDieResults(1,100) lt 50, int(arguments.AverageValue / local.Gems.Price), ceiling(arguments.AverageValue / local.Gems.Price)))>

		<cfreturn local.Gems>
	</cffunction>

<cfset local.CR = 2>
<cfset local.Value = 3500>
<cfset local.ProgressionID = 1>
<cfif StructKeyExists(form,"CR")>
	<cfset local.CR = form.CR>
</cfif>
<cfif StructKeyExists(form,"Value")>
	<cfset local.Value = form.Value>
</cfif>

<cftry>
<cfdump var="#local.Value#">
<cfset local.Treasure = GetCoinSplit(local.Value, local.CR)>
<!---<cfset local.Treasure = GetCoinsGemsArt(local.CR, local.ProgressionID, local.value)>--->
<cfdump var="#local.Treasure#">
<cfcatch>
	<cfdump var="#cfcatch#">
</cfcatch>
</cftry>
<form method="post">
	<input type="text" name="value" value="<cfoutput>#local.Value#</cfoutput>" />
	<br/>
	<select name="CR">
		<cfoutput>
        	<cfloop from="1" to="20" index="variables.i">
				<option value="#variables.i#"<cfif variables.i eq local.CR> selected="selected"</cfif>>#variables.i#</option>
			</cfloop>
        </cfoutput>
	</select>
	<br/>
	<button>Run!</button>
</form>